<?php

App::uses('AppController', 'Controller');

/**
 * ExtraPayments Controller
 *
 * @property ExtraPayment $ExtraPayment
 */
class ExtraPaymentsController extends AppController {

  /**
   * index method
   *
   * @return void
   */
  public function index() {
    $this->ExtraPayment->recursive = 0;
    $this->paginate = array('order' => 'ExtraPayment.date DESC');
    $this->set('extraPayments', $this->paginate());
  }

  /**
   * view method
   *
   * @throws NotFoundException
   * @param string $id
   * @return void
   */
  public function view($id = null) {
    if (!$this->ExtraPayment->exists($id)) {
      throw new NotFoundException(__('Invalid extra payment'));
    }
    $options = array('conditions' => array('ExtraPayment.' . $this->ExtraPayment->primaryKey => $id));
    $this->set('extraPayment', $this->ExtraPayment->find('first', $options));
  }

  /**
   * add method
   *
   * @return void
   */
  public function add() {
    if ($this->request->is('post')) {
      $this->ExtraPayment->create();
      if ($this->ExtraPayment->save($this->request->data)) {
        $this->Session->setFlash(__('Gasto Extra registrado correctamente'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('Gasto Extra no registrado'));
      }
    }
    $cashDailies = $this->ExtraPayment->CashDaily->find('list');
    $this->set(compact('cashDailies'));
  }

  /**
   * add method
   *
   * @return void
   */
  public function addAjax() {
    if ($this->request->is('post')) {
      $this->ExtraPayment->create();
      if ($this->ExtraPayment->save($this->request->data)) {
        $this->redirect(array('action' => 'index'));
      }
    }
  }

  /**
   * edit method
   *
   * @throws NotFoundException
   * @param string $id
   * @return void
   */
  public function edit($id = null) {
    if (!$this->ExtraPayment->exists($id)) {
      throw new NotFoundException(__('Invalid extra payment'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
      if ($this->ExtraPayment->save($this->request->data)) {
        $this->Session->setFlash(__('Gasto Extra registrado'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('Gasto Extra No registrado'));
      }
    } else {
      $options = array('conditions' => array('ExtraPayment.' . $this->ExtraPayment->primaryKey => $id));
      $this->request->data = $this->ExtraPayment->find('first', $options);
    }
    $cashDailies = $this->ExtraPayment->CashDaily->find('list');
    $this->set(compact('cashDailies'));
  }

  /**
   * delete method
   *
   * @throws NotFoundException
   * @throws MethodNotAllowedException
   * @param string $id
   * @return void
   */
  public function delete($id = null) {
    $this->ExtraPayment->id = $id;
    if (!$this->ExtraPayment->exists()) {
      throw new NotFoundException(__('Invalid extra payment'));
    }
    $this->request->onlyAllow('post', 'delete');
    if ($this->ExtraPayment->delete()) {
      $this->Session->setFlash(__('Gasto Extra Borrado'));
      $this->redirect(array('action' => 'index'));
    }
    $this->Session->setFlash(__('Gasto Extra NO borrado'));
    $this->redirect(array('action' => 'index'));
  }

}
